local thisTerminalsFloor = 1

rs.setOutput("left", false)
rs.setOutput("right", false)

while rs.getInput("left") or rs.getInput("right") do
        print("Appears lelevator is already in use. Please wait...")
        os.pullEvent("redstone")
        sleep(1)
end

local function findElevator()
        if rs.getInput("top") then return 1 end     -- Elevator at floor 1.
        if rs.getInput("back") then return 2 end    -- Elevator at floor 2.
        if rs.getInput("bottom") then return 3 end  -- Elevator at floor 3.
        return false
end

local function moveElevator(elevatorFloor, destFloor)
        if elevatorFloor ~= destFloor then
                print("Moving from floor "..tostring(elevatorFloor).." to floor "..tostring(destFloor).."...")

                local signalSide = destFloor < elevatorFloor and "right" or "left"

                repeat
                        rs.setOutput(signalSide, true)
                        os.pullEvent("redstone")
                        rs.setOutput(signalSide, false)
                until findElevator() == destFloor
        end
end

local input
while (not input) or input < 1 or input > 3 do
        print("Enter target floor (1 - 3):")
        input = tonumber(read())
end

local currentFloor = findElevator()
if not currentFloor then
        print("Can't determine lift location.") 
        repeat
                rs.setOutput("right", true)
                os.pullEvent("redstone")
                print("Got possible lift signal; rechecking...")
                rs.setOutput("right", false)
                currentFloor = findElevator()
        until currentFloor
end

moveElevator(currentFloor, thisTerminalsFloor)

print("Please board.")
sleep(5)

moveElevator(thisTerminalsFloor, input)

print("Operation complete.")
sleep(2)
os.shutdown()